package com.example.zzl.viewmodel.activity;

import android.content.Context;
import android.widget.RadioGroup;

import com.example.zzl.R;
import com.example.zzl.databinding.ActivityMainBinding;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.view.activity.MainActivity;
import com.example.zzl.view.fragment.CartFragment.CartFragment;
import com.example.zzl.view.fragment.HomeFragment.HomeFragment;
import com.example.zzl.view.fragment.SearchFragment.SearchFragment;
import com.example.zzl.view.fragment.ShareOrderFragment.ShareOrderFragment;
import com.example.zzl.view.fragment.UserFragment.UserFragment;
import com.example.zzl.viewmodel.BaseCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by ligg on 2017/2/7.
 */

public class MainActivityPresenter implements RadioGroup.OnCheckedChangeListener {

    private ActivityMainBinding db;
    private  Callback callback;
    private Context activity;
    private ArrayList<BaseFragment> fragments;

    public interface Callback extends BaseCallback{

        void changeFragment(int position);
    }

    public MainActivityPresenter(MainActivity activity, Callback callback, ActivityMainBinding db) {
        this.activity = activity;
        this.db = db;
        this.callback = callback;
    }

    public void init() {
        fragments = new ArrayList<>();
        HomeFragment homeFragment = HomeFragment.newInstance(null);
        SearchFragment searchFragment = SearchFragment.newInstance(null);
        ShareOrderFragment shareOrderFragment = ShareOrderFragment.newInstance(null);
        CartFragment cartFragment = CartFragment.newInstance(null);
        UserFragment userFragment = UserFragment.newInstance();
        Collections.addAll(fragments, homeFragment, searchFragment, shareOrderFragment, cartFragment, userFragment);
    }

    public void initView() {
        db.rg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.home:
                callback.changeFragment(0);
                break;

            case R.id.search:
                callback.changeFragment(1);
                break;

            case R.id.share_order:
                callback.changeFragment(2);
                break;

            case R.id.cart:
                callback.changeFragment(3);
                break;

            case R.id.user:
                callback.changeFragment(4);
                break;

            default:
                break;
        }
    }

    public List<BaseFragment> getFragment() {
        return fragments;
    }
}
